'''
Created on 23/09/2013

@author: Hugo sama
'''
import unittest
from SearchManager import SearchManager
from DocumentCollection import DocumentCollection
from InvertedIndex import InvertedIndex

class TestSearchManager(unittest.TestCase):
    searchManager =  SearchManager()
    
    def test_input_parser(self):
        inputText = "text & don't"
        parsedInput = self.searchManager.inputParser(inputText)
        self.assertEqual(["text","&","dont"],parsedInput, "No genera los \"AND\" o no normaliza tokens" )
        inputText2 = "(algo | otro) & thouse"
        parsedInput2 = self.searchManager.inputParser(inputText2)
        self.assertEqual(["(","algo","|","otro",")","&","thouse"], parsedInput2, "No genera los OR o los parentesis")
        inputText = "text don't"
        parsedInput = self.searchManager.inputParser(inputText)
        self.assertEqual(["text","&","dont"],parsedInput, "No genera los \"AND\" o no normaliza tokens" )  
        
    def test_infix_to_posfix_transformation(self):
        infix = ["text","&","dont"]
        postfix = self.searchManager.infixToPostfix(infix)
        self.assertEquals(["text","dont","&"], postfix, "No transforma a posfijo")
        infix2 = ["(","this","|","that",")","&","thouse"]
        postfix2 = self.searchManager.infixToPostfix(infix2)
        self.assertEquals(["this","that","|","thouse","&"], postfix2, "No transforma a posfijo")
        
    def test_postfixEvaluation(self):
        dc = DocumentCollection("")
        ii = InvertedIndex()
        documents = dc.collectDocuments();
        for document in documents:
            tokens = dc.getTokens(document)
            tokens = dc.normalizeTokens(tokens)
            ii.addWordsDocument(tokens, document)
#         parsedInput = self.searchManager.inputParser(" dignissim & libero | at & dictum & rutrum")
#         postfix = self.searchManager.infixToPostfix(parsedInput)
#         documents = self.searchManager.postfixEvaluation(postfix,ii)
#         self.assertEqual([0], documents, "No hace bien la evaluacion")
#         parsedInput = self.searchManager.inputParser(" -dignissim ")
#         postfix = self.searchManager.infixToPostfix(parsedInput)
#         documents = self.searchManager.postfixEvaluation(postfix,ii)
#         self.assertEqual([], documents, "No hace bien la evaluacion con negacion sola")
#         parsedInput = self.searchManager.inputParser(" -(-dignissim) ")
#         postfix = self.searchManager.infixToPostfix(parsedInput)
#         documents = self.searchManager.postfixEvaluation(postfix,ii)
#         self.assertEqual([0], documents, "No hace bien la evaluacion con doble negacion")
#         parsedInput = self.searchManager.inputParser(" -(-dignissim ")
#         postfix = self.searchManager.infixToPostfix(parsedInput)
#         documents = self.searchManager.postfixEvaluation(postfix,ii)
#         self.assertFalse(documents, "No hace bien la evaluacion de expresiones malformadas")
#         parsedInput = self.searchManager.inputParser(" &dignissim ")
#         postfix = self.searchManager.infixToPostfix(parsedInput)
#         documents = self.searchManager.postfixEvaluation(postfix,ii)
#         self.assertFalse(documents, "No hace bien la evaluacion de expresiones malformadas")
        
        
if __name__ == '__main__':
    unittest.main()